package com.impress;

import java.util.Timer;
import java.util.TimerTask;

import com.impress.model.AccountPreference;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

/**
 * 负责启动整个程序
 * @author pluto
 *
 */
public class Splash extends Activity {

	private Timer timer;
	private long startTime;
	private final static int TIME_UP = 0;
	private final static int INIT_ACCOUNT = TIME_UP + 1;
	
	private final TimerTask task = new TimerTask() {
		
		@Override
		public void run() {
			// TODO Initial resources.
			if (task.scheduledExecutionTime()-startTime >= 1000) {
				Message message = new Message();
								
				AccountPreference accountPreference = new AccountPreference(Splash.this);
				if (accountPreference.getAccount().IsEmpty()) {
					message.what = INIT_ACCOUNT;
				}
				else
					message.what = TIME_UP;
				timeHandler.sendMessage(message);
				timer.cancel();
				this.cancel();
			}
		}
	};
	
	private final Handler timeHandler = new Handler(){
		public void handleMessage(Message message){
			switch (message.what) {
			case INIT_ACCOUNT:
				Splash.this.finish();
				startActivity(new Intent(Splash.this, AccountLogin.class));
				break;
			case TIME_UP:
				Splash.this.finish();
				startActivity(new Intent(Splash.this, MainListActivity.class));
				break;

			default:
				break;
			}
			super.handleMessage(message);
		}
	};
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.splash);
		
		timer = new Timer(true);
		startTime = System.currentTimeMillis();
		timer.schedule(task, 0, 1);
	}

}
